package com.carrot.framework.listener;

import cn.hutool.core.thread.ThreadUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.event.ApplicationFailedEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.Environment;

import java.util.concurrent.TimeUnit;

/**
 * 系统启动失败
 *
 * @author liuhongzhen
 */
@Slf4j
public class ApplicationFailedEventListener implements ApplicationListener<ApplicationFailedEvent> {

    @Override
    public void onApplicationEvent(ApplicationFailedEvent event) {
        ConfigurableApplicationContext application = event.getApplicationContext();
        Environment env = application.getEnvironment();
        // 睡一秒打印
        ThreadUtil.sleep(1, TimeUnit.SECONDS);
        log.info("\n----------------------------------------------------------\n\t" +
                        "Application '{}' is failed to activate! Access URLs:\n\t" +
                        "Please check the relevant configuration!\n\t" +
                        "----------------------------------------------------------",
                env.getProperty("spring.application.name"));
    }

}
